//
//  EngagementProcessor.swift
//  btvideo
//
//  Created by Jason on 2021/7/28.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit




class ST_SuspendProcessor {
    
    
    static let `default` = ST_SuspendProcessor()
  
    // Can't init is singleton
    private init() {
    }
    
    fileprivate let animateDuration:TimeInterval = 0.25
    
    fileprivate(set) var sharePreView:UIView?
    
    fileprivate(set) var currentWindow:UIWindow?
    
    fileprivate(set) var temporaryController:ST_SuspendProcessorProtocol?
}

extension ST_SuspendProcessor{
    
    fileprivate func create_suspend_window()->ST_SuspendWindow{
        
        var window:ST_SuspendWindow
        if #available(iOS 13, *) {
            if let windowScene = ST_SuspendWindow.key_window_scene(){
                window = ST_SuspendWindow(windowScene: windowScene)
                window.frame = CGRect(origin: ST_SuspendConfig.default.suspend_default_point, size: ST_SuspendConfig.default.suspend_default_size)
            }else{
                window = ST_SuspendWindow(frame: CGRect(origin: ST_SuspendConfig.default.suspend_default_point, size: ST_SuspendConfig.default.suspend_default_size))
            }
        } else {
            window = ST_SuspendWindow(frame: CGRect(origin: ST_SuspendConfig.default.suspend_default_point, size: ST_SuspendConfig.default.suspend_default_size))
        }
        
        window.cornerRadius = 16
        window.backgroundColor = ST_SuspendConfig.default.window_background_color
        
        window.tap_recover_closure = { [weak self] in
            self?.window_tap_handle()
        }
        
        return window
    }
    
    
    fileprivate func window_tap_handle(){
        
        
        self.temporaryController?.suspendPrepareRecover()
        
        UIView.animate(withDuration: animateDuration, animations: {

            self.currentWindow?.frame = UIScreen.main.bounds
            
            self.temporaryController?.suspendRecoveringAnimate()
            
        }) { _ in
            
            self.temporaryController?.suspendRecoverComplete()

            self.destroy()
            
        }
        


        
        
    }
    
}


extension ST_SuspendProcessor{
    
    func destroy(){
        
        self.sharePreView = nil
        self.currentWindow?.isHidden = true
        self.currentWindow = nil
        self.temporaryController = nil
    }
    
    func suspend(controller:ST_SuspendProcessorProtocol){
        
        temporaryController = controller
        
        let toFrame = ST_SuspendConfig.default.window_default_frame.inset(by: ST_SuspendConfig.default.window_content_edge)
        
        controller.prepareSuspend(toFrame: toFrame)
                
        UIView.animate(withDuration: animateDuration, animations: {
            
            controller.suspendAnimating(toFrame: toFrame)
            
        }) { (complete) in
            
            guard let animateShareView = controller.suspendShareView() else{
                return
            }
            
            self.sharePreView = animateShareView
            
            let window = self.create_suspend_window()
                
            window.show()
            window.addSubview(animateShareView)
            animateShareView.snp.remakeConstraints { make in
                make.edges.equalToSuperview().inset(ST_SuspendConfig.default.window_content_edge)
            }
            
            
            self.currentWindow = window
            
            controller.suspendAnimateComplete()
            
        }
    }
}


//MARK: - Protocol
protocol ST_SuspendProcessorProtocol:NSObjectProtocol {
    
    func prepareSuspend(toFrame:CGRect)
    
    func suspendAnimating(toFrame:CGRect)
    
    func suspendAnimateComplete()
    
    func suspendShareView() -> UIView?
    
    func suspendPrepareRecover()
    
    func suspendRecoveringAnimate()
    
    func suspendRecoverComplete()
}
